import BasePageCtrl from "./BasePageCtrl";

const { ccclass, property } = cc._decorator;
@ccclass
export default class OutsideBeiJingGuidePageCtrl extends BasePageCtrl{

constructor() {
    this.ui = null;
}

onLoad() {
    this.onUILoad();
    this._animType =AnimType.SCALE;
    this._touchControl = false;
    this._hasPeneLock = true;
    this._hasBlack = false;
    this._hasTouchLock = true;
    this._hasBlackTouch = true;
    this._reuse = false;
    BasePageCtrl.prototype.onLoad.call(this);
    this.addButtonListen();
}

start() {
    l.reportData("show_beijing_guide");
    this.node.height = cc.winSize.height;
    this.node.width = cc.winSize.width;
    var isDmthFlag = p.isDmthFlag();
    var isHitOppoReviewBan = d.isHitOppoReviewBan();
    var tipText = "恭喜获得10000元红包！到达北京就可全部提现！";
    if (p.getDmth2Ab1()) {
        tipText = "恭喜获得10000元红包！到达终点就可全部提现！";
    } else if (p.getDmth2Ab2()) {
        tipText = "恭喜获得10000元红包！到达长城就可全部提现！";
    } else if (isDmthFlag) {
        if (isHitOppoReviewBan) {
            tipText = "恭喜获得10000元红包！到达终点就可全部提现！";
        } else {
            tipText = "恭喜获得10000元红包！到达京城就可全部提现！";
        }
    } else if (u.isOppoChannel()) {
        tipText = "恭喜获得10000元红包！到达终点就可全部提现！";
    }

    var guideTipComponent = this.ui.guideTip.getComponent(cc.RichText);
    guideTipComponent.string = "";
    this.typingAni(guideTipComponent, tipText, 150, () => {
        UiManage.addButtonListen(this.ui.closeBtn, this.clickClose, this, 0);
        UiManage.addButtonListen(this.ui.btn_continue, this.clickClose, this, 0);
    });
}

onUILoad() {
    this.ui = this.node.addComponent(OutsideBeiJingGuidePage);
}

addButtonListen() { }

typingAni(label, text, interval, callback) {
    const tagRegex = /<.+?\/?>/g;
    const tagMatches = text.match(tagRegex);
    const placeholderText = text.replace(tagRegex, "│").split("│");
    const labelArray = [];
    let tagCount = 0;

    for (let i = 0; i < placeholderText.length; i++) {
        let placeholder = placeholderText[i];
        if (placeholder !== "") {
            placeholder = "$[" + tagCount + "]";
            tagCount += 1;
        }
        labelArray.push(placeholder);
    }

    const joinedText = labelArray.join("│");

    for (let i = 0; i < placeholderText.length; i++) {
        if (placeholderText[i] === "") {
            placeholderText.splice(i, 1);
            i -= 1;
        }
    }

    while (joinedText.indexOf("│") !== -1) {
        if (tagMatches[0]) {
            joinedText = joinedText.replace("│", tagMatches[0].toString());
            tagMatches.splice(0, 1);
        } else {
            joinedText = joinedText.replace("│", "");
            console.warn("Not enough tagMatches");
        }
    }

    const newTextArray = [];
    const tagArray = new Array(tagCount).fill("");

    for (let i = 0; i < placeholderText.length; i++) {
        let placeholder = placeholderText[i];
        for (let j = 0; j < placeholder.length; j++) {
            let char = placeholder[j];
            tagArray[i] += char;
            let newText = joinedText;
            for (let k = 0; k < tagCount; k++) {
                newText = newText.replace("$[" + k + "]", tagArray[k]);
            }
            newTextArray.push(newText);
        }
    }

    let currentIndex = 0;

    function typing() {
        if (currentIndex >= newTextArray.length) {
            if (callback) {
                callback();
            }
        } else {
            label.string = newTextArray[currentIndex];
            currentIndex += 1;
            setTimeout(() => {
                typing();
            }, interval);
        }
    }

    setTimeout(() => {
        typing();
    }, interval);
}

clickClose() {
    this.hide();
}
}

OutsideBeiJingGuidePageCtrl.prefabUrl = "OutsideBeiJingGuidePage";
OutsideBeiJingGuidePageCtrl.className = "OutsideBeiJingGuidePageCtrl";